<style>
.pageFormContent .inputArea p {
    float: none;
    width: 800px;
}

#moveStockPage .lb {
    width: 150px;
    left: 480px;
    position: absolute;
}

#moveStockPage span.error {
    position: absolute !important;
    left: 260px;
}
</style>
<#include "/common/pagerForm.html">
<div class="pageContent">
    <div class="pageFormContent" id="moveStockPage">
        <div class="inputArea">
            <form rel="pagerForm" method="post" action=""
                class="pageForm required-validate">
                <p>
                    <label>移出仓库：</label> 
                    <select    id="out_warehouse" class="allyselect" ref="out_location" 
                        postField="warehousCode"
                        refUrl="${rc.getContextPath()}/manage/moveStock/getStorageLocations"
                        name="fromWarehouseCode" class="required"> 
                        <#list warehouses as warehouse>
                        <option value='${warehouse.warehouseCode}' locationFlag="${warehouse.locationManageFlag}">
                            ${warehouse.warehouseCode}-${warehouse.warehouseText}
                        </option> 
                        </#list>
                    </select> <label class="lb"><input type="checkbox" value="1">提交后重置移出仓库</label>
                </p>
                <p>
                    <label>移出库位：</label> <select
                        id="out_location" field="storageLocationCode"
                        name="fromLocationCode" label="storageLocationText"
                        class="required">
                    </select> <label class="lb"><input type="checkbox" value="1">提交后重置移出库位</label>
                </p>
                <p>
                    <label>移入仓库：</label> <select
                        id="in_warehouse" class="allyselect"
                        ref="in_location" postField="warehousCode"
                        refUrl="${rc.getContextPath()}/manage/moveStock/getStorageLocations"
                        name="toWarehouseCode" class="required"> 
                        <#list warehouses as warehouse>
                        <option value='${warehouse.warehouseCode}' locationFlag="${warehouse.locationManageFlag}">
                            ${warehouse.warehouseCode}-${warehouse.warehouseText}
                        </option> 
                        </#list>
                    </select> <label class="lb"><input type="checkbox" value="1">提交后重置移入仓库</label>
                </p>
                <p>
                    <label>移入库位：</label> <select
                        id="in_location" field="storageLocationCode"
                        name="toLocationCode" label="storageLocationText"
                        class="required">
                    </select> <label class="lb"><input type="checkbox" value="1">提交后重置移入库位</label>
                </p>
                <p>
                    <label><@label key="moveType.C,colon">移动类型：</@label></label> <select
                        id="moveType" name="moveTypeCode"
                        label="moveTypeText" class="required">
                    </select> <label class="lb"><input type="checkbox" value="1"><@label
                        key="resetMoveType.C">提交后重置移动类型</@label></label>
                </p>
                <p>
                    <label><@label key="material.C,colon">物料：</@label></label> <input
                        class="required look" id="materialLookup.materialCode"
                        name="materialCode" type="text" postField="materialCode"
                        suggestFields="materialCode,materialText"
                        suggestUrl="${rc.getContextPath()}/manage/material/componentSuggest"
                        remote="${rc.getContextPath()}/manage/material/validateMaterial" warn="<@label key='materialCodeNotExists.M'>物料不存在或已被使用</@label>"
                        lookupGroup="materialLookup" />
                    <a class="btnLook"
                        href="${rc.getContextPath()}/manage/material/componentLookUp?lookupType=radio"
                        rel="materialLookup_dialog"
                        title="<@label key='select.C,materialCode.C'>选择物料</@label>"
                        lookupGroup="materialLookup"></a> <input
                        style="margin-left: 15px;" id="materialLookup.materialText"
                        name="materialText" type="text" readonly="readonly" /> <label
                        class="lb"><input type="checkbox" value="1"><@label
                        key="resetMaterial.C">提交后重置物料</@label></label>
                </p>
                <p>
                    <label><@label key='quantity.C,colon'>数量：</@label></label> <input
                        type="text" name="quantity" class="required digits integer" min="1"
                        maxlength="15" /> <label class="lb"><input
                        type="checkbox" value="1" checked><@label
                        key="resetQuantity.C">提交后重置数量</@label></label>
                </p>
                <p>
                    <label><@label key="supplier.C,colon">供应商：</@label></label> <input
                        class="look" id="supplierLookup.supplierCode" name="supplierCode"
                        type="text" postField="supplierCode"
                        suggestFields="supplierCode,supplierText"
                        suggestUrl="${rc.getContextPath()}/manage/supplier/supplierSuggest"
                        remote="${rc.getContextPath()}/manage/supplier/validateSupplier"
                        warn="<@label key='supplier.C,doesNotExist.C'>供应商不存在</@label>"  
                        lookupGroup="supplierLookup" />
                    <a class="btnLook"
                        href="${rc.getContextPath()}/manage/supplier/supplierLookUp"
                        rel="materialLookup_dialog"
                        title="<@label key='select.C,supplier.C'>选择供应商</@label>"
                        lookupGroup="supplierLookup"></a> <input
                        style="margin-left: 15px;" id="supplierLookup.supplierText"
                        name="supplierText" type="text" readonly="readonly" /> <label
                        class="lb"><input type="checkbox" value="1"><@label
                        key="resetSupplier.C">提交后重置供应商</@label></label>
                </p>
                <p>
                    <label><@label key='lotNum.C,colon'>批次号：</@label></label> <input
                        type="text" name="lotNum" maxlength="40" /> <label class="lb"><input
                        type="checkbox" value="1"><@label
                        key="resetLotNum.C">提交后重置批号</@label></label>
                </p>

                <div class="pageContent" style="padding: 5px 0;">
                    <ul>
                        <li>
                            <div class="buttonActive" style="margin-left: 110px;">
                                <div class="buttonContent">
                                    <button id="moveStockBtn" type="button" >移库</button>
                                </div>
                            </div>
                        </li>
                        <li>
                            <div class="buttonActiveGrey" style="margin-left: 50px;">
                                <div class="buttonContent">
                                    <button id="formRest" type="reset"><@label key="reset.B">重置</@label></button>
                                </div>
                            </div>
                        </li>
                    </ul>
                </div>
            </form>
            <p>
                <label style="width: 108px">
                <@label    key="opLog.C">操作日志：保留最近</@label></label> 
                <input id="logRowNums" style="width: 50px" size="22" type="text" maxlength="60"    class="integer number" value="100" /> 
                <label><@label key="record.C">条记录</@label></label>
            </p>
        </div>
        <div>
            <table class="table border" width="100%" layoutH="390" style="overflow: scroll;">
                <thead>
                    <tr>
                        <th width="100">
                            <@label    key="material.C">物料</@label>
                        </th>
                        <th width="100">
                            移出仓库
                        </th>
                        <th width="100">
                            移出库位
                        </th>
                        <th width="100">
                            移入仓库
                        </th>
                        <th width="100">
                            移入库位
                        </th>
                        <th width="100">
                            <@label    key="quantity.C">数量</@label>
                        </th>
                        <th width="100">
                            <@label    key="supplier.C">供应商</@label>
                        </th>
                        <th width="100">
                            <@label    key="lotNum.C">批次号</@label>
                        </th>
                        <th width="140">
                            <@label    key="opTime.C">操作时间</@label>
                        </th>
                    </tr>
                </thead>
                <tbody id="logTbody">
                    <tr>
                        <td class="noData">======<@label key="noListData.C">列表无数据</@label>=====</td>
                    </tr>
                </tbody>
            </table>
        </div>
    </div>
</div>


<script type="text/javascript">
(function() {
    $(function(){
        var $page = $('#moveStockPage'),
        isSuccessFul = false,
        $tboty = $('#logTbody', $page),
        $trTem = $('<tr/>')
        .append('<td ng="materialText"/>')
        .append('<td ng="fromWarehouseCode"/>')
        .append('<td ng="fromLocationCode"/>')
        .append('<td ng="toWarehouseCode"/>')
        .append('<td ng="toLocationCode"/>')
        .append('<td ng="quantity"/>')
        .append('<td ng="supplierText"/>')
        .append('<td ng="lotNum"/>')
        .append('<td/>'),
        appendLog = function(data) {
            if (!isSuccessFul) {
                $tboty.empty();
                isSuccessFul = true;
            }
            var $tr = $trTem.clone();
            $.each(data, function(){
                var $tag = $('[name=' + this.name + ']');
                if ($tag.is('select')) {
                    $('td[ng=' + this.name + ']', $tr).append($('<div/>').append($('option:selected', $tag).text()));
                } else {
                    $('td[ng=' + this.name + ']', $tr).append($('<div/>').append(this.value));
                }
            });
            $('td:last', $tr).append($('<div/>').append(new Date().formatDate('yyyy-MM-dd HH:mm:ss')));
            $tr.prependTo($tboty);
            var logRowNums = parseInt($('#logRowNums', $page).val()) || 100;
            if (logRowNums < 1) {
                $('#logRowNums', $page).val(100);
                logRowNums = 100;
            }
            $('tr:gt(' + (logRowNums - 1) + ')', $tboty).remove();
            $.jTableTool.initGridColumnWidth($tboty.parents('.grid:first'));
        },
        $submit = $('#moveStockBtn', $page).on('click', function(){
            var $form = $(this).parents('form:first');
            if (!$form.valid()) {
                return false;
            }
            var postData = $form.serializeArray();
            if ($('[name=fromWarehouseCode]', $form).val() == $('[name=toWarehouseCode]', $form).val() 
                    && $('[name=fromLocationCode]', $form).val() == $('[name=toLocationCode]', $form).val()) {
                alertMsg.error('相同的仓库或者库位不能移库！');
                return false;
            }
            $.ajax({
                url: '${rc.getContextPath()}/manage/moveStock/saveMoveStock',
                type: 'POST',
                dataType : "json",
                data: postData,
                async: false,
                cache: false,
                success: function(data) {
                    DWZ.ajaxDone(data);
                    if (data.statusCode == DWZ.statusCode.ok) {    
                        appendLog(postData);
                        // 根据checkbox重置数据
                        $.each($('p > label > :checkbox', $form), function(){
                            if (this.checked) {
                                var $this = $(this), $p = $this.parent().parent();
                                $('select option:eq(0)', $p).attr('selected', 'selected').parent().change();
                                $('input[type=text]', $p).val('');
                            }
                        });
                    }
                },
                error : DWZ.ajaxError
            });
        }),
        $rest = $('#formRest', $page).on('click', function(){
            var $form = $(this).parents('form:first');
            $.each($('p', $form), function(){
                var $p = $(this);
                $('select option:eq(0)', $p).attr('selected', 'selected').parent().change();
            });
        }),
        $out_warehouse = $('#out_warehouse', $page),
        $in_warehouse = $('#in_warehouse', $page),
        $out_location = $('#out_location', $page),
        $in_location = $('#in_location', $page),
        $moveType = $('#moveType', $page),
        
        getCommonMoveTypes = function(outWarehousCode, inWarehousCode) {
            if (outWarehousCode && inWarehousCode) {
                $.DWZajax({
                    url: '${rc.getContextPath()}/manage/moveStock/getCommonMoveTypes',
                    type: 'POST',
                    dataType : "json",
                    data: {
                        outWarehousCode: outWarehousCode,
                        inWarehousCode: inWarehousCode
                    },
                    async: false,
                    cache: false,
                    callback: function(data) {
                        $moveType.empty();
                        $.each(data, function() {
                            var $option = $('<option/>').attr('value', this.moveTypeCode).text(this.moveTypeText);
                            $moveType.append($option);
                        });
                    }
                });
            }
        },
        setLayouth = function () {
            var layoutH = 390;
            if ($out_location.is(':hidden')) {
                layoutH -= 31;
            }
            if ($in_location.is(':hidden')) {
                layoutH -= 31;
            }
            $('.gridScroller', $page).attr('layouth', layoutH);
            $(window).resize();
        };
        
        $out_warehouse.on('select', function() {
            var $this = $(this), selectedIndex = $this[0].selectedIndex, 
            locationFlag = $('>option:eq(' + selectedIndex + ')', $this).attr('locationFlag') == 1;
            $('option:hidden', $in_warehouse).show();
            if (!locationFlag) {
                $out_location.removeClass('required').parent().hide();
                $('option[value=' + $this.val() + ']', $in_warehouse).hide();
                if ($in_warehouse[0].selectedIndex == selectedIndex) {
                    if (selectedIndex == 0) {
                        $in_warehouse[0].selectedIndex = 1;
                    } else {
                        $in_warehouse[0].selectedIndex = 0;
                    }
                }
            } else {
                $out_location.addClass('required').parent().show();
            }
        });
        
        $in_warehouse.on('select', function() {
            var $this = $(this), selectedIndex = $this[0].selectedIndex,
            locationFlag = $('>option:eq(' + selectedIndex + ')', $this).attr('locationFlag') == 1;
            if (!locationFlag) {
                $in_location.removeClass('required').parent().hide();
            } else {
                $in_location.addClass('required').parent().show();
            }
        });
        
        $('#out_warehouse, #in_warehouse', $page).on('select', function(){
            setLayouth();
            var out_warehouse = $out_warehouse.val(), in_warehouse = $in_warehouse.val();
            getCommonMoveTypes(out_warehouse, in_warehouse);
        });
    });
})(jQuery);
</script>
